<?php

namespace Wechat\Controller;

class RankController extends CommonController {

    public function index() {

        $map["banner_type"] = C("BANNER_TYPE.RANK");

        $bannerList = D("Banner")
            ->where($map)
            ->order("banner_id desc")
            ->select();

        $this->assign("bannerList", $bannerList);
        $this->display("index");

    }

    public function rankByWeek() {

        $week = date('w') == 0 ? 7 : date('w');
        $beginDate = date('Y-m-d',strtotime( '+'. 1-$week .' days' ));
        $endDate = date('Y-m-d',strtotime( '+'. 8-$week .' days' ));

        $sql = "select ifnull(g.win,0) as win, ifnull(g.total,0) as total,
                c.nickname,c.headimgurl, 
                sum(pr.point) as points from c_point_record pr 
                left join (
                select customer_id,count(customer_id) as total, 
                sum(case when status = 3 then 1 else 0 end) as win 
                from c_group 
                where status <> -1 and 
                create_time BETWEEN '$beginDate' and '$endDate' 
                group by customer_id
                ) g on g.customer_id = pr.customer_id
                left join c_customer c on c.customer_id = pr.customer_id 
                where pr.create_time BETWEEN '$beginDate' and '$endDate' 
                group by pr.customer_id order by sum(pr.point) desc limit 30";

        $list = M()->query($sql);

        echo json_encode($list);

    }

    public function rankByMonth() {

        $beginDate = date('Y-m-1');
        $endDate = date('Y-m-1', strtotime('+1 month'));

        $sql = "select ifnull(g.win,0) as win, ifnull(g.total,0) as total,
                c.nickname,c.headimgurl, 
                sum(pr.point) as points from c_point_record pr 
                left join (
                select customer_id,count(customer_id) as total, 
                sum(case when status = 3 then 1 else 0 end) as win 
                from c_group 
                where status <> -1 and 
                create_time BETWEEN '$beginDate' and '$endDate' 
                group by customer_id
                ) g on g.customer_id = pr.customer_id
                left join c_customer c on c.customer_id = pr.customer_id 
                where pr.create_time BETWEEN '$beginDate' and '$endDate' 
                group by pr.customer_id order by sum(pr.point) desc limit 30";

        $list = M()->query($sql);

        echo json_encode($list);

    }

    public function rank() {

        $sql = "select ifnull(g.win,0) as win, ifnull(g.total,0) as total,
                c.nickname,c.headimgurl, 
                sum(pr.point) as points from c_point_record pr 
                left join (
                select customer_id,count(customer_id) as total, 
                sum(case when status = 3 then 1 else 0 end) as win 
                from c_group 
                where status <> -1  
                group by customer_id
                ) g on g.customer_id = pr.customer_id
                left join c_customer c on c.customer_id = pr.customer_id 
                group by pr.customer_id order by sum(pr.point) desc limit 30";

        $list = M()->query($sql);

        echo json_encode($list);

    }

}